package com.tgpms.web.cost.controller;

import com.tgpms.common.Result;
import com.tgpms.exception.DeleteException;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.exception.ProjectException;
import com.tgpms.exception.QueryException;
import com.tgpms.exception.UpdateException;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.PageView;
import com.tgpms.web.cost.entity.EstimateHierarchyMaster;
import com.tgpms.web.cost.service.EstimateHierarchyMasterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author May
 * <p>
 * 概算层次相关接口
 */
@Api(value = "EstimateHierarchyMasterController", tags = "概算层次相关接口")
@RestController
@RequestMapping(value = "/ev/estimateHierarchyMaster")
public class EstimateHierarchyMasterController {

    @Resource
    private EstimateHierarchyMasterService estimateHierarchyMasterService;

    /**
     * 查询所有概算层次代码并做分页处理
     *
     * @param estimateHierarchyCode 概算层次代码
     * @param description           描述
     * @param dataOwner             拥有者
     * @param pageNo                分页参数
     * @return pageView
     */
    @ApiOperation(value = "查询所有概算层次代码并做分页处理", notes = "查询所有概算层次代码并做分页处理", httpMethod = "POST")
    @PostMapping(value = "/queryAll")
    public Result queryAll(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
			@RequestParam(value = "condition", required = false) String condition,
		    @RequestParam(value = "dataowner", required = false) String dataowner,
		    @RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize) {
    	Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = estimateHierarchyMasterService.queryAll(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
    }

    /**
     * 增加概算层次代码相关数据
     *
     * @param estimateHierarchyMaster 概算层次实体
     * @return null
     */
    @ApiOperation(value = "增加概算层次代码相关数据", notes = "增加概算层次代码相关数据", httpMethod = "POST")
    @PostMapping("/addEstimateHierarchyMaster")
    public Result addEstimateHierarchyMaster(@RequestBody EstimateHierarchyMaster estimateHierarchyMaster) {
        Result result = new Result();
        if(Common.isEmpty(estimateHierarchyMaster.getEstimateHierarchyCode())) {
        	result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
        }
        try {
        	Map<String,String> map = estimateHierarchyMasterService.addEstimateHierarchyMasterInfo(estimateHierarchyMaster);
        	if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
        } catch (Exception e) {
        	e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        
        return result;
    }
    
    
    /**
     * 更新概算层次代码相关数据
     *
     * @param estimateHierarchyMaster 概算层次实体
     * @return null
     */
    @ApiOperation(value = "更新概算层次代码相关数据", notes = "更新概算层次代码相关数据", httpMethod = "PUT")
    @PutMapping("/updateEstimateHierarchyMaster")
    public Result updateEstimateHierarchyMaster(@RequestBody EstimateHierarchyMaster estimateHierarchyMaster) {
        Result result = new Result();
        if(Common.isEmpty(estimateHierarchyMaster.getEstimateHierarchyCode())) {
        	result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
        }
        try {
        	Map<String,String> map = estimateHierarchyMasterService.updateEstimateHierarchyMasterInfo(estimateHierarchyMaster);
        	if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
        } catch (Exception e) {
        	e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    /**
     * 根据层次代码删除对应的数据
     *
     * @param estimateHierarchyCode 概算层次码
     * @return null
     */
    @ApiOperation(value = "根据层次代码删除对应的数据", notes = "根据层次代码删除对应的数据", httpMethod = "DELETE")
    @DeleteMapping("/delEstimateHierarchyMaster")
    public Result delEstimateHierarchyMaster(@RequestParam(value="estimateHierarchyCode", required=true) String estimateHierarchyCode) {
        Result result = new Result();
        if(Common.isEmpty(estimateHierarchyCode)) {
        	result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
        }
        try {
        	Map<String, String> map = estimateHierarchyMasterService.delEstimateHierarchyMaster(estimateHierarchyCode);
        	if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
        } catch (Exception e) {
        	e.printStackTrace();
        	result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }

    /**
     * 查询菜单
     *
     * @param estimateHierarchyCode 概算ID
     * @param levelNo 层次
     * @param parentCode 父级
     * @return null
     */
    @ApiOperation(value = "查询菜单", notes = "查询菜单", httpMethod = "POST")
    @PostMapping("/findKeyByMenu")
    public Result findKeyByMenu(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "condition", required = false) String condition,
			@RequestParam(value = "dataowner", required = false) String dataowner,
			@RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize) {
    	Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			List<Map<String,Object>> list = estimateHierarchyMasterService.findKeyByMenu(pageInfo);
			result.setSuccess(true);
			result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
    }

}
